<?php
include '../../include/config.php';
include '../../include/Mysql.Class.php';

class reportFunction
{
    // 上行是卖 下行是买
    function getTopBarData()
    { // 按年统计
        $mysql = new mysql($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'], $GLOBALS['mydbname'], $GLOBALS['conn'], $GLOBALS['mydbcharset']);
        $sql = "select goodName as Xdata,round(sum(moneyCount)/10000,4) as Ydata from reportrecord where upYear = year(now())";
        $sql = $sql . " and goodType = 'S' group by goodName order by sum(moneyCount) DESC limit 0,20";
        $result = $mysql->query($sql);
        $results = array();
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;
        }
        return $results;
    }

    function getBottomBarData()
    { // 按年统计
        $mysql = new mysql($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'], $GLOBALS['mydbname'], $GLOBALS['conn'], $GLOBALS['mydbcharset']);
        $sql = "select goodName as Xdata,round(sum(moneyCount)/10000,4) as Ydata from reportrecord where upYear = year(now())";
        $sql = $sql . " and goodType = 'B' group by goodName order by sum(moneyCount) DESC limit 0,20";
        $result = $mysql->query($sql);
        $results = array();
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;
        }
        return $results;
    }

    function getTopPieData()
    { // 统计前一天数据
        $mysql = new mysql($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'], $GLOBALS['mydbname'], $GLOBALS['conn'], $GLOBALS['mydbcharset']);
        $sql = "select name as name,round(sum(moneyCount)/10000,4) as value from reportrecord where upYear = year(DATE_ADD(now(),INTERVAL -1 DAY))";
        $sql = $sql . " and upMonth = MONTH(DATE_ADD(now(),INTERVAL -1 DAY)) and upDay = DAY(DATE_ADD(now(),INTERVAL -1 DAY))";
        $sql = $sql . " and goodType = 'S' group by name order by sum(moneyCount) DESC limit 0,20";
        $result = $mysql->query($sql);
        $results = array();
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;
        }
        return $results;
    }

    function getBottomPieData()
    { // 统计前一天数据
        $mysql = new mysql($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'], $GLOBALS['mydbname'], $GLOBALS['conn'], $GLOBALS['mydbcharset']);
        $sql = "select name as name,round(sum(moneyCount)/10000,4) as value from reportrecord where upYear = year(DATE_ADD(now(),INTERVAL -1 DAY))";
        $sql = $sql . " and upMonth = MONTH(DATE_ADD(now(),INTERVAL -1 DAY)) and upDay = DAY(DATE_ADD(now(),INTERVAL -1 DAY))";
        $sql = $sql . " and goodType = 'B' group by name order by sum(moneyCount) DESC limit 0,20";
        $result = $mysql->query($sql);
        $results = array();
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;
        }
        return $results;
    }

    function getLineData()
    { // 按年统计
        $mysql = new mysql($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'], $GLOBALS['mydbname'], $GLOBALS['conn'], $GLOBALS['mydbcharset']);
        $sql = "select   
                round(sum(case month(uploaddate) when '1' then moneycount else 0 end)/10000,4) as '1',  
                round(sum(case month(uploaddate) when '2' then moneycount else 0 end)/10000,4) as '2',  
                round(sum(case month(uploaddate) when '3' then moneycount else 0 end)/10000,4) as '3',  
                round(sum(case month(uploaddate) when '4' then moneycount else 0 end)/10000,4) as '4',  
                round(sum(case month(uploaddate) when '5' then moneycount else 0 end)/10000,4) as '5',  
                round(sum(case month(uploaddate) when '6' then moneycount else 0 end)/10000,4) as '6',  
                round(sum(case month(uploaddate) when '7' then moneycount else 0 end)/10000,4) as '7',  
                round(sum(case month(uploaddate) when '8' then moneycount else 0 end)/10000,4) as '8',  
                round(sum(case month(uploaddate) when '9' then moneycount else 0 end)/10000,4) as '9',  
                round(sum(case month(uploaddate) when '10' then moneycount else 0 end)/10000,4) as '10',  
                round(sum(case month(uploaddate) when '11' then moneycount else 0 end)/10000,4) as '11',  
                round(sum(case month(uploaddate) when '12' then moneycount else 0 end)/10000,4) as '12'  
                from reportrecord  
                where year(uploaddate)=year(now())";
        $result = $mysql->query($sql . " and goodType = 'S'");
        $results = array();
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;//上行数据
        }
        $result = $mysql->query($sql . " and goodType = 'B'");
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;//下行数据
        }
        return $results;
    }
    //前一天的买卖合计
    function getYesterdayData()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select sum(moneyCount) as value from reportrecord where upYear = year(DATE_ADD(now(),INTERVAL -1 DAY))";
        $sql = $sql . " and upMonth = MONTH(DATE_ADD(now(),INTERVAL -1 DAY)) and upDay = DAY(DATE_ADD(now(),INTERVAL -1 DAY))";
        $result = $mysql->query($sql . " and goodType = 'S'");
        $yesterday = mysqli_fetch_assoc($result);
        $rtdata['sell']=round($yesterday['value']); //前一天的卖合计
        $result = $mysql->query($sql . " and goodType = 'B'");
        $yesterday = mysqli_fetch_assoc($result);
        $rtdata['buy']=round($yesterday['value']);//前一天的买合计
        return $rtdata;
    }
    //当天的买卖合计
    function getTodayData()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select sum(moneyCount) as value from reportrecord where upYear = year(now()) and upMonth = month(now()) and upDay = day(now())";
        $result = $mysql->query($sql . " and goodType = 'S'");
        $day = mysqli_fetch_assoc($result);
        $rtdata['sell']=round($day['value']);//当天的卖合计
        $result = $mysql->query($sql . " and goodType = 'B'");
        $day = mysqli_fetch_assoc($result);
        $rtdata['buy']=round($day['value']);//当天的买合计
        return $rtdata;
    }
    
    //当月的买卖合计
    function getMonthData()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select sum(moneyCount) as value from reportrecord where upMonth = month(now())";
        $result = $mysql->query($sql . " and goodType = 'S'");
        $month = mysqli_fetch_assoc($result);
        $rtdata['sell'] = round($month['value']);//当月的卖合计
        $result = $mysql->query($sql . " and goodType = 'B'");
        $month = mysqli_fetch_assoc($result);
        $rtdata['buy'] = round($month['value']);//当月的买合计
        return $rtdata;
    }
    
    //当年的买卖合计
    function getYearData()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select sum(moneyCount) as value from reportrecord where upYear = year(now())";
        $result = $mysql->query($sql . " and goodType = 'S'");
        $year = mysqli_fetch_assoc($result);
        $rtdata['sell'] = round($year['value']);//一年的卖合计
        $result = $mysql->query($sql . " and goodType = 'B'");
        $year = mysqli_fetch_assoc($result);
        $rtdata['buy'] = round($year['value']);//一年的买合计
        return $rtdata;
    }
    
    //当年的商品排行榜
    function getYearTop()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select goodName,sum(moneyCount) as value from reportrecord where upYear = year(now())";
        $result = $mysql->query($sql . " and goodType = 'S' group by goodName order by value desc limit 0,1");
        $year = mysqli_fetch_assoc($result);
        if(empty($year['goodName']))
        {
            $rtdata['sell_top'] = "无";
        }else{
            $rtdata['sell_top'] = $year['goodName'];
        }
        $result = $mysql->query($sql . " and goodType = 'B' group by goodName order by value desc limit 0,1");
        $year = mysqli_fetch_assoc($result);
        if(empty($year['goodName']))
        {
            $rtdata['buy_top'] = "无";
        }else{
            $rtdata['buy_top'] = $year['goodName'];
        }

        return $rtdata;
    }
    
    //当天的商品排行榜
    function getTodayTop()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select goodName,sum(moneyCount) as value from reportrecord where upYear = year(now()) and upMonth = month(now()) and upDay = day(now())";
        $result = $mysql->query($sql . " and goodType = 'S' group by goodName order by value desc limit 0,1");
        $day = mysqli_fetch_assoc($result);
        if(empty($day['goodName']))
        {
            $rtdata['sell_top'] = "无";
        }else{
            $rtdata['sell_top'] = $day['goodName'];
        }
        
        $result = $mysql->query($sql . " and goodType = 'B' group by goodName order by value desc limit 0,1");
        $day = mysqli_fetch_assoc($result);
        if(empty($day['goodName']))
        {
            $rtdata['buy_top'] = "无";
        }else{
            $rtdata['buy_top'] = $day['goodName'];
        }
        return $rtdata;
    }
    //昨天的商品排行榜
    function getYesterdayTop()
    {
        $mysql =  new mysqli($GLOBALS['mydbhost'], $GLOBALS['mydbuser'], $GLOBALS['mydbpw'],$GLOBALS['mydbname']);
        $sql = "select goodName,sum(moneyCount) as value from reportrecord where upYear = year(DATE_ADD(now(),INTERVAL -1 DAY))";
        $sql = $sql . " and upMonth = MONTH(DATE_ADD(now(),INTERVAL -1 DAY)) and upDay = DAY(DATE_ADD(now(),INTERVAL -1 DAY))";
        $result = $mysql->query($sql . " and goodType = 'S' group by goodName order by value desc limit 0,1");
        $yesterday = mysqli_fetch_assoc($result);
        if(empty($yesterday['goodName']))
        {
            $rtdata['sell_top'] = "无";
        }else{
            $rtdata['sell_top'] = $yesterday['goodName'];
        }
        $result = $mysql->query($sql . " and goodType = 'B' group by goodName order by value desc limit 0,1");
        $yesterday = mysqli_fetch_assoc($result);
        
        if(empty($yesterday['goodName']))
        {
            $rtdata['buy_top'] = "无";
        }else{
            $rtdata['buy_top'] = $yesterday['goodName'];
        }
        return $rtdata;
    }
}
?>